package com.qnw.CardGroupManagement;

import android.os.*;
import android.support.v4.app.*;
import android.support.v4.util.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.*;
import cn.bmob.v3.*;
import cn.bmob.v3.exception.*;
import cn.bmob.v3.listener.*;
import com.qnw.CardGroupManagement.*;
import com.qnw.CardGroupManagement.adapter.*;
import com.ygo.feihua.*;
import com.ygo.feihua.BmobTable.*;
import java.util.*;
import com.ygo.feihua.Management.*;
import com.ygo.feihua.util.*;

public class FragmentZiliao extends Fragment
{
	private TextView xg;

	private Gongju gj;
	private DialogUtils du;
	private UserManagement um;
	
	private String zlid=null;
	boolean isziliao=false;
	boolean iscx=false;
	
	String km;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View v=inflater.inflate(R.layout.fragment_xiaoguo,null);

		xg=(TextView) v.findViewById(R.id.xg);
		A5 a5=(A5)getActivity();
		km=a5.getCard().getId();

		gj=Gongju.getdx(getActivity());
		du=DialogUtils.getdx(getActivity());
		xg.setOnClickListener(new OnClickListener(){

				
				@Override
				public void onClick(View p1)
				{
					um=UserManagement.getDx();
					if(um.getUser()!=null){
						if(um.getUser().getUser_qx()==1){
							if(iscx){
							View[] v=du.dialoge(false,"","请输入卡片资料");
							final EditText et=(EditText) v[0];
							Button bt=(Button) v[1];
							if(isziliao){
								et.setText(xg.getText().toString());
							}
							bt.setOnClickListener(new OnClickListener(){

									@Override
									public void onClick(View p1)
									{
										CardInformation ci=new CardInformation();
										ci.setCard_km(km);
											ci.setCard_sh(2);
											ci.setCard_type(1);
											ci.setCard_message(et.getText().toString().trim());
											if(isziliao){
											ci.update(zlid, new UpdateListener(){

													@Override
													public void done(BmobException p1)
													{
														du.dis();
														if(p1==null){
															gj.show("添加成功");
														}else{
															gj.show("添加失败,原因为"+p1);
														}
														// TODO: Implement this method
													}
												});
											}else{
										ci.save(new SaveListener<String>(){

												@Override
												public void done(String p1, BmobException p2)
												{
													du.dis();
													if(p2==null){
														gj.show("添加成功");
													
													}else{
														gj.show("添加失败,原因为"+p2);
													}
													// TODO: Implement this method
												}
											});
										
										}
										
										// TODO: Implement this method
									}
								});
							}else{
								gj.show("查询该卡资料失败,请退出该界面重新进入");
							}
						}
					}
					// TODO: Implement this method
				}
			});
		
		BmobQuery<CardInformation> bq=new BmobQuery<CardInformation>();
		bq.include("-createdAt");
		bq.addWhereEqualTo("card_sh", 1);
		bq.addWhereEqualTo("card_type",1);
		bq.addWhereEqualTo("card_km", km);
		bq.findObjects(new FindListener<CardInformation>(){

				@Override
				public void done(List<CardInformation> p1, BmobException p2)
				{
					if (p2 == null)
					{
						if (p1.size() != 0)
						{
							xg.setText(p1.get(0).getCard_message());
							isziliao=true;
							zlid=p1.get(0).getObjectId();
						}
						else
						{
							xg.setText("未查询到该卡资料");
						}
						iscx=true;
					}else{
						xg.setText("查询卡片信息失败,原因为"+p2);
						
						}

					// TODO: Implement this method
				}
			});


		// TODO: Implement this method
		return v;
	}


}
